package com.mllweb.blog.core.notice.email;

import com.mllweb.blog.core.notice.email.template.ActivateTemplate;
import com.mllweb.blog.core.notice.email.template.EmailTemplate;
import org.springframework.stereotype.Component;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

@Component
public class Email {
    private final String EMAIL_HOST = "mail.smtp.host";
    private final String EMAIL_AUTH = "mail.smtp.auth";
    private final String EMAIL_PROTOCOL = "mail.transport.protocol";

    public void send(String validateUser, EmailTemplate template) {
        Properties properties = new Properties();
        Authenticator authenticator = new MailAuthenticator();
        properties.setProperty(EMAIL_PROTOCOL, EmailConfig.EMAIL_PROTOCOL);
        properties.put(EMAIL_HOST, EmailConfig.EMAIL_HOST);
        properties.put(EMAIL_AUTH, EmailConfig.EMAIL_AUTH);

        //  设置对话和邮件服务器进行通讯
        Session session = Session.getDefaultInstance(properties, authenticator);
        //  设置邮件对象
        Message message = new MimeMessage(session);
        try {
            message.setSentDate(new Date());
            Address address = new InternetAddress(EmailConfig.FROM, EmailConfig.FROM_NAME);
            message.setFrom(address);
            Address toAddress = new InternetAddress(validateUser);
            message.setSubject(template.subject());
            message.setRecipient(Message.RecipientType.TO, toAddress);
            message.setContent(template.body(), template.contentType());
            message.saveChanges();
//           发送邮件
            System.out.println("doSendHtmlMail() 开始发送邮件……");
            Transport.send(message);
            System.out.println("发送成功！");
        } catch (MessagingException e) {
            e.printStackTrace();
            System.out.println("错误！" + e.getMessage());
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            System.out.println("错误！" + e.getMessage());
        }

    }

    /**
     * 发送带有附件格式的邮件
     */
//    public void doSendAttachmentMail() {
//
//        BodyPart messageBodyPart = new MimeBodyPart();
//        messageBodyPart.setText("bodypart");
//
//        Multipart multipart = new MimeMultipart();
//        multipart.addBodyPart(messageBodyPart);
//
//        messageBodyPart = new MimeBodyPart();
//
////          设置上传的资源
//        DataSource source = new FileDataSource("E:\\3.doc");
////          添加到
//        messageBodyPart.setDataHandler(new DataHandler(source));
////          设置文件名称,记得后缀名
//        messageBodyPart.setFileName("test.doc");
//        multipart.addBodyPart(messageBodyPart);
//
//        message.setContent(multipart);
//    }



}